/* 
> --------------------------------------------------------------------------------- 
> -- Copyright (C) THALES AIR SYSTEMS SA 2005-2011 all rights reserved. 
> -- This software is the property of THALES AIR SYSTEMS SA and may not be used in 
> -- any manner except under a licence agreement signed with THALES AIR SYSTEMS SA. 
> --------------------------------------------------------------------------------- 
*/ 
package utils;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.xml.parsers.SAXParser;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.SAXException;
import org.xml.sax.Attributes;
import javax.xml.parsers.SAXParserFactory;

public class ParserXML {
		
	/** Parse a .xml file and return the wanted data as a mapping of strings.
	 * @param fileName the XML file to be parsed.
	 * @return a mapping between the wanted strings.
	 */
	
	public static Map<String, String> extract(String fileName) {
		
		System.out.println("ParserXML parsing file : " + fileName);
		
		Map<String, String> result = new HashMap<String, String>();
		List<String> resultKey = new LinkedList<String>();
		List<String> resultValue = new LinkedList<String>();
		
		try {
			// Create and return a new instance of SAX-implementation:
			SAXParserFactory factory = SAXParserFactory.newInstance();

			// Create a SAX-parser...
			SAXParser parser = factory.newSAXParser();
			
			// .. define our handler:
			SaxHandler handler = new SaxHandler();

			// and parse:
			parser.parse(fileName, handler);
			
			// We are parsing dcps.xml
			if (handler.listTopic.size() > 0) {
				resultKey = handler.listTopic;
				resultValue = handler.listType;
			}
			
			// We are parsing DDSConfig.xml
			else if (handler.listDDS.size() > 0) {
				resultKey = handler.listDDS;
				resultValue = handler.listSize;
			}
			
			else {
				throw new Exception("ERROR : no list initialized when parsing " + fileName);
			}
			
			int tailleKey = resultKey.size();
			int tailleValue = resultValue.size();
			
			// The two lists shall have the same size to map them.
			if (tailleKey == tailleValue) {
				for (int i = 0; i < tailleKey ; i++) {
					result.put(resultKey.get(i), resultValue.get(i));
				}
			}
			else {
				throw new Exception("ERROR : erroneous parsing of " + fileName + " : uncorrect mapping of keys and values");
			}
		} 
		catch (Exception ex) {
			ex.printStackTrace(System.out);
		}
		return result;
	}

	/** Local class that defines our own implementation of SAX handler reading.
	 * @version 2.0
	 * @author Copyright (C) THALES AIR SYSTEMS SA 2005-2011 all rights reserved.
	 */
	
	private static final class SaxHandler extends DefaultHandler {
		// Lists used for dcps.xml
		protected List<String> listTopic = new LinkedList<String>();
		protected List<String> listType = new LinkedList<String>();
		
		// Lists used for DDSConfig.xml
		protected List<String> listDDS = new LinkedList<String>();
		protected List<String> listSize = new LinkedList<String>();
		
		public void startElement(String uri, String localName,
				String qName, Attributes attrs) throws SAXException {
			
			// 1°) To parse the file dcps.xml
			if (qName.equals("topic")) {
				String nameTopic = attrs.getValue("name");
				listTopic.add(nameTopic);
			}
			
			// Get the file path of the DDS from dcps.xml
			if (qName.equals("type")) {
				String nameType = attrs.getValue("name");
				listType.add(nameType);
			}
			
			// 2°) To parse the file DDSConfig.xml
			if (qName.equals("dds")) {
				listDDS.add(attrs.getValue("topic"));
				listSize.add(attrs.getValue("size"));
			}
		}
	}
}
